/**
 * Project Name:rx-java
 * File Name:RetryDemo.java
 * Package Name:top.xiaocao.rx.java.filter.function
 * Date:2024年2月13日下午6:35:53
 * Copyright (c) 2024, zhengchong.wan@minicore.com.hk All Rights Reserved.
 *
*/

package top.xiaocao.rx.java.filter.function;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Predicate;

/**
 * @className:RetryDemo 
 * @comment: 注释信息 
 * @date:     2024年2月13日 下午6:35:53 
 * @author   zhengchong.wan
 * @since    V1.0.0	 
 */
public class RetryDemo {
	
	public static void main(String[] args) {
		Observable.create(new ObservableOnSubscribe<Integer>() {
			@Override
			public void subscribe(ObservableEmitter<Integer> e) {
				e.onNext(1);
				e.onNext(2);
				e.onError(new Exception("抛出异常"));
			}
		})
				//重新发送数据，这里重发2次，带有异常信息回调
				.retry(2, new Predicate<Throwable>() {
					@Override
					public boolean test(Throwable throwable) {
						//false: 不重新发送数据，回调Observer的onError()方法结束
						//true: 重新发送请求（最多发送2次）
						return true;
					}
				}).subscribe(new Observer<Integer>() {
					@Override
					public void onSubscribe(Disposable d) {
						System.out.println("retry：onSubscribe == 订阅");
					}

					@Override
					public void onNext(Integer integer) {
						System.out.println("retry：onNext ==" + integer);
					}

					@Override
					public void onError(Throwable e) {
						System.out.println("retry：onError == " + e.getMessage());
					}

					@Override
					public void onComplete() {
						System.out.println("retry：onComplete == ");
					}
				});
	}

}

